<?php
namespace app\admin\controller;
class Goods extends Base {
    //列表
    public function lists() {
        if (is_post()) {
            $d = $this->post;
            $map[] = ['and','website_id','=', $this->web_site_id];
            if ($d['keyword'] != '') {
                $map[] = ['and', 'cat_title', 'like', "%{$d['keyword']}%"];
            }
            $w = where($map);
            $rs = $this->db->query("select * from goods $w[0] order by sort desc,id desc" . limit($d['page'], $d['limit']), $w[1], '', true);
            if ($rs) {
                foreach ($rs[0] as $key => $val) {
                  $rs[0][$key]['status'] = c("type.status")[$val['status']];
                  $rs[0][$key]['create_time'] = date('Y-m-d H:i', $val['create_time']);
                }
                suc($rs[0], $rs[1]);
            } else {
                suc([], 0);
            }
        }
        $cat = $this->db->query("select id, cat_title from goods_cat");
        $this->fetch(['cat'=>$cat]);
    }

    //添加
    public function add() {
        if (is_post()) {
            $d = $this->post;
            $rule = [
                'cat_title'=>'require',
            ];
            $msg = [];
            check($rule,$msg,$d);
            $d['website_id'] = $this->web_site_id;
            $d['create_time'] = time();
            $this->db->insert('goods', $d);
            cookie('js_msg', '添加成功');
            cookie('js_function', 'preload');
            suc();
        }
        $this->fetch();
    }

    //编辑
    public function edit() {
        if (is_post()) {
            $d = $this->post;
            $rule = [
                'cat_title'=>'require',
            ];
            $msg = [];
            check($rule,$msg,$d);
            $d['website_id'] = $this->web_site_id;
            $d['create_time'] = time();
            $this->db->update('goods', $d);
            cookie('js_msg', '修改成功');
            cookie('js_function', 'preload');
            suc();
        }
        $id = $this->get['id'];
        $info = $this->db->query("select * from goods where id=?", [$id], 1);
        $this->fetch(['info' => $info]);
    }
}
